package com.jackeysun.util

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.StringRes
import com.jackeysun.util.Application.mApplication

/**
 *   Created by sunxunchao on 2021/10/29.
 */
fun Context.toast(msg: String, duration: Int = Toast.LENGTH_LONG) {
    Toast.makeText(this, msg, duration).show()
}

fun Context.toast(@StringRes msg: Int, duration: Int = Toast.LENGTH_LONG) {
    Toast.makeText(this, msg, duration).show()
}

fun Context.middleToast(msg: String, duration: Int = Toast.LENGTH_LONG) {
    val mToast = Toast(this)
    val view: View = LayoutInflater.from(this).inflate(R.layout.toast_view, null)
    val messageTv: TextView = view.findViewById(R.id.message_tv)
    messageTv.text = msg
    mToast.view = view
    mToast.duration = duration
    mToast.setGravity(Gravity.CENTER, 0, 0)
    mToast.show()
}

fun Context.middleToast(@StringRes id: Int, duration: Int = Toast.LENGTH_LONG) {
    val mToast = Toast(this)
    val view: View = LayoutInflater.from(this).inflate(R.layout.toast_view, null)
    val messageTv: TextView = view.findViewById(R.id.message_tv)
    messageTv.text = this.resources.getString(id)
    mToast.view = view
    mToast.duration = duration
    mToast.setGravity(Gravity.CENTER, 0, 0)
    mToast.show()
}


fun String.toast(duration: Int = Toast.LENGTH_LONG) {
    Toast.makeText(mApplication, this, duration).show()
}

fun Int.toast(duration: Int = Toast.LENGTH_LONG) {
    Toast.makeText(mApplication, this, duration).show()
}

fun String.middleToast(duration: Int = Toast.LENGTH_LONG) {
    val mToast = Toast(mApplication)
    val view: View = LayoutInflater.from(mApplication).inflate(R.layout.toast_view, null)
    val messageTv: TextView = view.findViewById(R.id.message_tv)
    messageTv.text = this
    mToast.view = view
    mToast.duration = duration
    mToast.setGravity(Gravity.CENTER, 0, 0)
    mToast.show()
}

fun Int.middleToast(duration: Int = Toast.LENGTH_LONG) {
    val mToast = Toast(mApplication)
    val view: View = LayoutInflater.from(mApplication).inflate(R.layout.toast_view, null)
    val messageTv: TextView = view.findViewById(R.id.message_tv)
    messageTv.text = mApplication.getString(this)
    mToast.view = view
    mToast.duration = duration
    mToast.setGravity(Gravity.CENTER, 0, 0)
    mToast.show()
}


